package com.work.filter;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.work.domain.AppVersion;
import com.work.domain.UserAdmin;
import com.work.mapper.AppVersionMapper;
import com.work.util.Constant;

/**
 * 
 * @ClassName: ValidateFilter
 * @Description: 登陆权限过滤器
 * @author 朱冰艺
 * @date 2019年1月5日 上午16:33:35
 *
 */

public class ValidateFilter implements Filter {
	
    private ServletContext servletContext;

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse rep = (HttpServletResponse) response;
        HttpSession session = req.getSession();

        // 1.可以进行配置(用户访问的页面,都可以在web.xml中进行配置)

        // 获取配置的登录页面地址
        String login_page = servletContext.getInitParameter("login_page");
        // 获取我们需要登录才能访问的页面地址
        String validate_page = servletContext.getInitParameter("validate_page");

        // 获取共用的页面(不需要可以就访问的页面地址)
        String common_page = servletContext.getInitParameter("common_page");

        // 获取当前用户访问的路径地址
        String current_url = req.getServletPath();
        
        //获取当前session登陆账户
        UserAdmin ua = (UserAdmin) session.getAttribute("adminuser");
        
        String contextPath = req.getContextPath();
        

        String url = req.getRequestURI();
		url = url.replace(contextPath, "");
		//web后台拦截器
		if (url.startsWith("/companyWeb") && !url.contains("/companyWeb/companyIndexWeb") && !url.contains("/companyWeb/companyIndexDetail") 
				&& !url.contains("/company") || url.contains("/jsps/main.jsp")|| url.startsWith("/jsps/company")|| url.startsWith("/jsps/userLog")){
			UserAdmin adminUser = (UserAdmin) req.getSession().getAttribute(Constant.SESSION_ADMIN_USER);
			if (adminUser == null) {
				System.out.println("拦截成功！");
				rep.sendRedirect(contextPath + "/login.jsp");
				return;
			}
			// SessionThreadLocal.setThreadSysUser(sysUser); url.startsWith("/UserAdmin") || 
		}
		//android端拦截器
		/*if(url.startsWith("/company")){
			AppVersion appv=new AppVersion();
			String versionid = appv.getVersionid();
			System.out.println("客户端versionid："+versionid);
			String findLastVersinId = avm.findLastVersinId();
			System.out.println("数据库versionid："+findLastVersinId);
			if(versionid.equals(findLastVersinId)){
				System.out.println("版本相同！");
			}else{
				System.out.println("版本不一样！");
			}
			
			System.out.println("拦截成功！");												
			//rep.sendRedirect(contextPath + "/login.jsp");
			return;
			
		}*/
        chain.doFilter(req, rep);

    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        servletContext = config.getServletContext();
    }

}
